package edu.put.sk.nrpp.ui;


import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JMenu;

import edu.put.sk.nrpp.ConnectionList;
import edu.put.sk.nrpp.Network;
import edu.put.sk.nrpp.NetworkOSPF;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.BorderLayout;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;

public class StartupWindow {
	private JFrame frmSetupProgram;
	JTextField textField;

	/**
	 * Create the application.
	 */
	public StartupWindow() {
		initialize();
	}
	private void initialize() {
		setFrame(new JFrame());
		getFrame().setBounds(100, 100, 300, 200);
		getFrame().setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmSetupProgram.getContentPane().setLayout(null);
		
		textField = new JTextField();
		textField.setBounds(56, 21, 223, 28);
		frmSetupProgram.getContentPane().add(textField);
		textField.setColumns(10);
		
		JLabel lblIp = new JLabel("IP:");
		lblIp.setBounds(6, 27, 61, 16);
		frmSetupProgram.getContentPane().add(lblIp);
		
		JButton btnStart = new JButton("Start");
		btnStart.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				try {
					//System.out.println(textField.getText());
					final Network n=new NetworkOSPF(textField.getText());
					Thread t=new Thread(n);
					t.setDaemon(true);
					t.start();
					new Thread(new Runnable(){
						@Override
						public void run(){
							try {
								ExampleWindow window = new ExampleWindow();
								window.getFrame().setVisible(true);
								n.addObserver(window);
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
					}).run();
						
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		btnStart.setBounds(162, 99, 117, 29);
		frmSetupProgram.getContentPane().add(btnStart);
		
	}

	/**
	 * @param frame the frame to set
	 */
	public void setFrame(JFrame frame) {
		this.frmSetupProgram = frame;
		frmSetupProgram.setTitle("Setup");
	}

	/**
	 * @return the frame
	 */
	public JFrame getFrame() {
		return frmSetupProgram;
	}
}
